﻿using System;
using System.Collections.Generic;
using System.Web;
using Omes.Model;
using Omes.Services;

namespace Omes.Admin
{
    public class jsserver : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string meth = context.Request.HttpMethod;
            switch (meth)
            {
                case "GET":
                    {
                        this.Get(context);
                    } break;
                case "POST":
                    {
                        this.Post(context);
                    }
                    break;
            }
        }

        private void Get(HttpContext context)
        {
            string result = string.Empty;
            string path = context.Request.Path.ToLower();
            //去掉path在web.config中的部分
            int index = path.IndexOf('/', 1);
            path = path.Substring(index);
            switch (path)
            {
                case "/page/delete":
                    {
                        int id = Convert.ToInt32(context.Request.QueryString["id"]);
                        DbServer db = new DbServer();
                        result = db.DeletePage(id).ToString();
                    } break;
                case "/all/menutype":
                    {
                        Omes.Services.DbServer db = new DbServer();
                        List<PageType> pageTypes = db.GetAllPageTypes();
                        result = Serializer.JsonSerializer<List<PageType>>(pageTypes);
                    } break;
                case "/all/parentpage":
                    {
                        Omes.Services.DbServer db = new DbServer();
                        List<Page> pages = db.GetAllPages();
                        result = Serializer.JsonSerializer<List<Page>>(pages);
                    }break;
            }
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.Write(result);
        }

        private void Post(HttpContext context)
        {
            string result = string.Empty;
            string path = context.Request.Path.ToLower();
            //去掉path在web.config中的部分
            int index = path.IndexOf('/', 1);
            path = path.Substring(index);
            switch (path)
            {
                case "/page/update":
                    {
                        int id = Convert.ToInt32(context.Request.Params["id"]);
                        int type = Convert.ToInt32(context.Request.Params["type"]);
                        int parent = Convert.ToInt32(context.Request.Params["parent"]);
                        string bar = context.Request.Params["bar"];
                        string windows = context.Request.Params["window"];
                        DbServer db = new DbServer();
                        result = db.UpdatePage(id, type, parent, bar, windows).ToString();
                    } break;
            }
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.Write(result);
        }
        #endregion
    }
}
